import { combineReducers } from 'redux';
import { configureStore } from '@reduxjs/toolkit';
import reduxLogger from 'redux-logger';
import reduxThunk from 'redux-thunk';
import { persistReducer, persistStore } from 'redux-persist';
import storage from 'redux-persist/lib/storage';
import taskSliceReducer from './features/taskSlice';
import globalSliceReducer from './features/globalSlice';
import { useDispatch, useSelector, TypedUseSelectorHook } from 'react-redux';

const persistConfig = {
  key: 'root',
  storage,
  blacklist: [], // 缓存黑名单
};

const reducers = combineReducers({
  task: taskSliceReducer,
  global: globalSliceReducer,
});

const persistedReducer = persistReducer(persistConfig, reducers);

const store = configureStore({
  // 指定reducer
  reducer: persistedReducer,
  devTools: process.env.NODE_ENV !== 'production',
  // 使用中间件
  middleware: [reduxLogger, reduxThunk],
});

export default store;
export const persist = persistStore(store);

export type RootState = ReturnType<typeof store.getState>;
export type AppDispatch = typeof store.dispatch;
export const useAppDispatch: () => AppDispatch = useDispatch;
export const useAppSelector: TypedUseSelectorHook<RootState> = useSelector;
